/**
 * Copyright (c) 2013-Now http://aidex.vip All rights reserved.
 */
import store from '@/store'
import config from '@/common/config.js'
// 此处第二个参数vm，就是我们在页面使用的this，你可以通过vm获取vuex等操作
const install = (Vue, vm) => {
	// 通用请求头设定
	const ajaxHeader = 'x-ajax';
	const sessionIdHeader = 'Authorization';
	const rememberMeHeader = 'x-remember';
	// 请求参数默认配置
	uni.$u.http.setConfig((config) => {
	  config.baseURL='http://171.220.244.246:8090/prod-api/'
	  config.timeout = 10000
	  return config
	})
	const urlArr=[
		'/core/facility/recharge/queryPayStatusBatch',
		'/core/facility/platform-pay-order/payOrderQueryBatch',
		'/core/facility/oilOrder/list',
		'/core/facility/platform-pay-order/oilOrder/list',
		'/system/facility/gun/list',
		'/system/facility/offline-gun-list',
		'/system/facility/selectList',
		'/system/facility/getStationByCompany'
		]
		const url='/system/facility/customer'
	// 请求拦截，配置Token等参数
	uni.$u.http.interceptors.request.use(req=>{
		console.log(req)
			req.header["source"] = "app";
			if(urlArr.indexOf(req.url)<0&&req.url.indexOf(url)<0){
				uni.showLoading({
					title: '加载中...'
				})
				
			}
			// 默认指定返回 JSON 数据
			if (!req.header[ajaxHeader]){
				req.header[ajaxHeader] = 'json';
			}
			
			// 设定传递 Token 认证参数 aidex
			if (store.state.vuex_token){
				req.header[sessionIdHeader] = store.state.vuex_token;
			}
			if (store.state.company_no){
				req.header['company_no'] = store.state.company_no;
			}
			if (store.state.sn){
				req.header['sn_no'] = store.state.sn;
			}
			if (store.state.login_name){
				req.header['login_name'] = store.state.login_name;
			}
			if(store.state.customerNo){
				req.header['customer_no']=store.state.customerNo
			}
			// 为节省流量，记住我数据不是每次都发送的，当会话失效后，尝试重试登录 aidex
			// if (!req.header[rememberMeHeader] && vm.vuex_remember && req.remember){
			// 	req.header[rememberMeHeader] = vm.vuex_remember;
			// 	req.remember = false;
			// }
		    console.log('request', req);
			return req;
		
	})
	
	// 响应拦截，判断状态码是否通过 (res, req)
	uni.$u.http.interceptors.response.use((res)=>{
		
		    console.log('response', res.data);
			
			uni.hideLoading()
			let data = res.data;
			if (!(data)){
				
				setTimeout(()=>{
					uni.showToast({
						title:'未连接到服务器', 
						icon: 'none',
					})
				},100)
			}
			if(res.data.code==='401'){
				// uni.$u.toast(res.data.msg)
				uni.$u.vuex('vuex_token', '');
				uni.$u.vuex('company_no', '');
				uni.$u.vuex('login_name', '');
				setTimeout(() => {
					uni.reLaunch({
						url: '/pages/login/login'
					});
				}, 500);
			}
			
			if(res.data.code!=='200'&&res.data.code!=='0'&&res.data.code!=='201'){
				if(res.data.msg){
					setTimeout(()=>{
						uni.showToast({
							title: res.data.msg, 
							icon: 'none',
						})
					},100)
				}else{
					if(res.data.code==='202'){
						uni.showToast({
							title:'支付失败', 
							icon: 'none',
						})
					}else{
						setTimeout(()=>{ 
							uni.showToast({
								title:'服务器繁忙', 
								icon: 'none',
							})
						},100)
					}
					
				}		
				return  res.data
			}else{
				return data
			}
			
	},error=>{
		uni.hideLoading()
	})
	
	
	
}

export default {
	install
}



